package GUI;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import client.ClientConsole;

//import Client.ProjectClient;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

@SuppressWarnings("serial")
/**
 * Connects to a specific running server
 * @author Benny
 *
 */
public class ConnectionGUI extends JPanel {
	private JTextField textIP;
	private JTextField textPort;
	public MainFrame mf;
	public JLabel lblMsg; 
	public ConnectionGUI() {
		setLayout(null);
		
		JLabel lblServerIp = new JLabel("Server IP:");
		lblServerIp.setBounds(10, 12, 74, 14);
		add(lblServerIp);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setBounds(10, 38, 74, 14);
		add(lblPort);
		
		textIP = new JTextField();
		textIP.setText("localhost");
		textIP.setBounds(94, 9, 119, 20);
		add(textIP);
		textIP.setColumns(10);
		
		textPort = new JTextField();
		textPort.setText("5555");
		textPort.setBounds(94, 35, 119, 20);
		add(textPort);
		textPort.setColumns(10);
		
		JButton btnNewButton = new JButton("Connect");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					mf.client=new ClientConsole(textIP.getText(), Integer.decode(textPort.getText()), mf);
					setVisible(false);
					mf.lblConnectedToServer.setVisible(true);
					mf.lg.btnLogin.setEnabled(true);
					mf.lg.setVisible(true);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (IOException e) {
					lblMsg.setText("Unable to connect to server");
					lblMsg.setForeground(Color.RED);
					
				}
			}
		});
		btnNewButton.setBounds(64, 64, 89, 23);
		add(btnNewButton);
		
		 lblMsg = new JLabel(" ");
		lblMsg.setBounds(45, 210, 203, 38);
		add(lblMsg);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad.png"));
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(227, 100);
		add(picLabel);
	}

}
